document.addEventListener("DOMContentLoaded", function () {
  const getCookieBtn = document.getElementById("getCookieBtn");
  const copyBtn = document.getElementById("copyBtn");
  const cookieList = document.getElementById("cookieList");
  const mpValue = document.getElementById("mpValue");

  // 获取Cookie按钮点击事件
  getCookieBtn.addEventListener("click", function () {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      const currentTab = tabs[0];
      const url = new URL(currentTab.url);

      chrome.cookies.getAll({ domain: ".jd.com" }, function (cookies) {
        if (cookies.length === 0) {
          cookieList.textContent = "没有找到Cookie";
          mpValue.textContent = "";
          return;
        }

        // 查找mp参数的cookie
        const mpCookie = cookies.find((cookie) => cookie.name === "mp");
        if (mpCookie) {
          mpValue.textContent = `账户ID：${mpCookie.value}`;
        } else {
          mpValue.textContent = "未找到账户ID";
        }

        const cookieText = cookies
          .map((cookie) => `${cookie.name}=${cookie.value}`)
          .join(";");

        cookieList.textContent = cookieText;
      });
    });
  });

  // 复制功能
  copyBtn.addEventListener("click", function () {
    const cookieText = cookieList.textContent;
    navigator.clipboard
      .writeText(cookieText)
      .then(function () {
        alert("Cookie已复制到剪贴板！");
      })
      .catch(function (err) {
        alert("复制失败：" + err);
      });
  });
});
